package com.xiaochong.tir.manager.scheduled;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.po.KLineBroaderIndex;
import com.xiaochong.tir.common.data.po.Report;
import com.xiaochong.tir.common.data.po.SyResearchReportFile;
import com.xiaochong.tir.common.data.vo.CurrencyInfoIndexVo;
import com.xiaochong.tir.common.data.vo.KLineBroaderIndexVo;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.DateUtils;
import com.xiaochong.tir.common.util.HbaseTableUtils;
import com.xiaochong.tir.common.util.HbaseUtils;
import com.xiaochong.tir.manager.component.HbaseBaseQueryComponent;
import com.xiaochong.tir.manager.service.CurrencyInfoService;
import com.xiaochong.tir.manager.service.KLineBroaderIndexService;
import com.xiaochong.tir.manager.service.ReportService;
import com.xiaochong.tir.manager.util.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/6 17:32
 */
@Component
public class ReportScheduled {

    @Autowired
    private OSSTool ossTool;
    @Autowired
    private KLineBroaderIndexService kLineBroaderIndexService;
    @Autowired
    private ReportService reportService;

    @Scheduled(cron="0 0 1 * * ?")//每天1点生成报表
    public void createReport(){
        LogTrace.beginTrace("生成数据报表");
        Map<String,Map<String,Object>> map = new HashMap<String,Map<String,Object>>();
        //文档名称
        String fileName = "币通数据维护文档"+LocalDate.now().minusDays(1).format(DateUtils.format_ymds)+".xls";
        //sheet名
        String sheetName = "币通数据维护文档";
        //头部标题
        String[] firstTitle = {"大盘指数每日数据","币种指数每日数据"};
        //小标题
        String[] title = {"日期（天）","开盘（大盘指数）","最高（大盘指数）","最低值（大盘指数）","收盘值（大盘指数）","成交额（总）","变动率（今收-昨日收）/昨日收）"};
        HSSFWorkbook wb = null;
        String[][] content = null;
        Map<String, Object> map1 = null;
                //大盘指数
        KLineBroaderIndexVo broaderVo = kLineBroaderIndexService.getBroaderIndex();
        if(null != broaderVo){
            //内容
            content = new String[1][7];
            content[0][0] = broaderVo.getDate();
            content[0][1] = broaderVo.getOpenPrice();
            content[0][2] = broaderVo.getHighestPrice();
            content[0][3] = broaderVo.getLowestPrice();
            content[0][4] = broaderVo.getClosePrice();
            content[0][5] = broaderVo.getVolumeCapCny();
            content[0][6] = broaderVo.getRateChange();
        }
        map1 = new HashMap<>();
        map1.put("title1", firstTitle[0]);
        map1.put("title2", title);
        map1.put("content", content);
        map.put("1",map1);
        //创建HSSFWorkbook
        wb = ExcelUtil.getHSSFWorkbook(sheetName, map, null,0);

        String[] title1 = {"日期（天）","货币名称","开盘（币种价格）","最高（币种价格）","最低值（币种价格）","收盘值（币种价格）","成交量","变动率（今收-昨日收）/昨日收）"};
        //币种指数
        List<CurrencyInfoIndexVo> list = kLineBroaderIndexService.getCurrencyIndexList();
        if(null != list){
            content = new String[list.size()][8];
            for (int i = 0; i < list.size(); i++) {
                CurrencyInfoIndexVo vo = list.get(i);
                content[i][0] = vo.getDate();
                content[i][1] = vo.getCoinName();
                content[i][2] = vo.getOpenPrice();
                content[i][3] = vo.getHighestPrice();
                content[i][4] = vo.getLowestPrice();
                content[i][5] = vo.getClosePrice();
                content[i][6] = vo.getTurnover();
                content[i][7] = vo.getRateChange();
            }
        }
        map1 = new HashMap<>();
        map1.put("title1", firstTitle[1]);
        map1.put("title2", title1);
        map1.put("content", content);
        map.put("1",map1);
        wb = ExcelUtil.getHSSFWorkbook(sheetName, map, wb,4);
        ByteArrayOutputStream os =null;
        InputStream stream = null;
        try {
            os = new ByteArrayOutputStream();
            wb.write(os);
            byte[] bytes =os.toByteArray();
            stream = new ByteArrayInputStream(bytes);
            boolean flag = ossTool.upload(stream,fileName);
            Report file = new Report();
            if(flag){
                file.setReportName(fileName);
                file.setCreateTime(new Date());
                file.setUpdateTime(new Date());
            }
            reportService.insertExcel(file);
            LogTrace.endTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != stream ){stream.close();}
                if(null != os){os.close();}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
